/**
 * 测试 STL Algorithm std::for_each()
 *
 */
#include <iostream>  // std::cout
#include <algorithm> // std::for_each
#include <vector>    // std::vector

void myfunction(int i) // function:
{
    std::cout << ' ' << i;
}

struct myclass // function object type:
{
    void operator()(int i) { std::cout << ' ' << i; }
} myobject;

int main()
{
    std::vector<int> myvector = {10, 20, 30};

    std::cout << "myvector contains:";
    for_each(myvector.begin(), myvector.end(), myfunction);
    std::cout << '\n';

    // or:
    std::cout << "myvector contains:";
    for_each(myvector.begin(), myvector.end(), myobject);
    std::cout << '\n';

    // or:
    std::cout << "myvector double:";
    for_each(myvector.begin(), myvector.end(), [](int a) {
        std::cout << ' ' << 2*a;
    });
    std::cout << '\n';

    for(auto e : myvector)
        std::cout << ' ' << e;
    std::cout << std::endl;

    return 0;
}